﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class UserAuthLogForm : Form
    {
        SqlConnection sqlCon = new SqlConnection(UserLog.connectionstring);
        SqlDataAdapter sda = new SqlDataAdapter();

        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn1 = new DataGridViewTextBoxColumn();
        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn2 = new DataGridViewTextBoxColumn();
        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn3 = new DataGridViewTextBoxColumn();
        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn4 = new DataGridViewTextBoxColumn();
        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn5 = new DataGridViewTextBoxColumn();
        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn6 = new DataGridViewTextBoxColumn();
        private System.Windows.Forms.DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn1 =new DataGridViewCheckBoxColumn();
        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn7 = new DataGridViewTextBoxColumn();
        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn8 = new DataGridViewTextBoxColumn();
        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn9 = new DataGridViewTextBoxColumn();
        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn10 = new DataGridViewTextBoxColumn();

        public UserAuthLogForm()
        {
            InitializeComponent();
        }

        private void UserAuthLogForm_Load(object sender, EventArgs e)
        {

            DataTable da = new DataTable();
            string strSql = "select time, authID, authname, authedID, authedname, state , detail from tb_authlog";
            SqlCommand cmd = new SqlCommand(strSql, sqlCon);
            sda.SelectCommand = cmd;
            sda.Fill(da);
            BindingSource bindingSource1 = new BindingSource();
            bindingSource1.DataSource = da;

            this.dataGridView1.DataSource = bindingSource1;
            this.bindingNavigator1.BindingSource = bindingSource1;

            this.dataGridView1.Columns[0].HeaderText = "时间";
            this.dataGridView1.Columns[1].HeaderText = "授权用户号码";
            this.dataGridView1.Columns[2].HeaderText = "授权用户姓名";
            this.dataGridView1.Columns[3].HeaderText = "被授权用户号码";
            this.dataGridView1.Columns[4].HeaderText = "被授权用户姓名";
            this.dataGridView1.Columns[5].HeaderText = "状态";
            this.dataGridView1.Columns[6].HeaderText = "详细信息";

            this.dataGridView1.Columns[0].ReadOnly = true;
            this.dataGridView1.Columns[1].ReadOnly = true;
            this.dataGridView1.Columns[2].ReadOnly = true;
            this.dataGridView1.Columns[3].ReadOnly = true;
            this.dataGridView1.Columns[4].ReadOnly = true;
            this.dataGridView1.Columns[5].ReadOnly = true;
            this.dataGridView1.Columns[6].ReadOnly = true;

            this.dateTimePicker1.Value = DateTime.Now;
            this.dateTimePicker2.Value = DateTime.Now;
            this.checkBox1.Checked = false;
           
            
        }

        private string buildsqlstring()
        {
            string strSql = "select time, authID, authname, authedID, authedname, state, detail from tb_authlog where itemid > 0 ";
            if(this.textBox1.Text != "")
            {
                strSql += " and authID = '" + this.textBox1.Text + @"'"; 
            }
            if(this.textBox2.Text != "")
            {
                strSql += " and " + @"authname = '" + this.textBox2.Text + @"'";
            }
            if (this.textBox3.Text != "")
            {
                strSql += " and " + @"authedID = '" + this.textBox3.Text + @"'";
            }
            if (this.textBox4.Text != "")
            {
                strSql += " and " + @"authedname = '" + this.textBox4.Text + @"'";
            }
 
            if(this.checkBox1.Checked)
            {
                DateTime starttime = this.dateTimePicker1.Value;
                DateTime endtime = this.dateTimePicker2.Value;

                endtime = endtime.AddHours(23);
                endtime = endtime.AddMinutes(59);
                endtime = endtime.AddMinutes(59);

                if (starttime <= endtime)
                {
                    strSql += " and " + @"time >=  '" + starttime.ToShortDateString() + @"' and  " + @"time <=  '" + endtime.ToShortDateString() + @"'";
                }
                else
                {
                    MessageBox.Show("开始时间必须小于结束时间");
                    return null;
                }
            }
     //       MessageBox.Show(strSql);
            return strSql;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable da = new DataTable();
            string strSql = this.buildsqlstring();
            if (strSql == null)
            {
                return;
            }
            SqlCommand cmd = new SqlCommand(strSql, sqlCon);
            sda.SelectCommand = cmd;
            sda.Fill(da);
            BindingSource bindingSource1 = new BindingSource();
            bindingSource1.DataSource = da;
            this.dataGridView1.DataSource = bindingSource1;
            this.bindingNavigator1.BindingSource = bindingSource1;
        }

        private void UserLoginLogForm_FormClosed(object sender, FormClosedEventArgs e)
        {

        }
    }
}
